var gulp = require('gulp');
var less = require('gulp-less');
var runSequence = require('run-sequence');
var sourcemaps = require('gulp-sourcemaps');
var rename = require('gulp-rename');
var LessAutoprefix = require('less-plugin-autoprefix');
var minifycss = require('gulp-minify-css');
var browsersync = require('browser-sync');

var autoprefix = new LessAutoprefix({ browsers: ['last 2 versions', 'IE > 8', '> 5%'], cascade: true });

var config = {
    dev: "./",
    src: "./lib/**/index.less",
    watch: "./lib/**/*.less",
    dest: "./dist/",
    maps: "./maps/"
}

gulp.task('less', function() {
    gulp.src(config.src)
        .pipe(sourcemaps.init())
        .pipe(less())
        //.pipe(minifycss()) //执行压缩
        .pipe(rename({ suffix: '.min' })) //rename压缩后的文件名
        .pipe(sourcemaps.write(config.maps))
        .pipe(gulp.dest(config.dest));
});


gulp.task("watch", function() {
    gulp.watch(config.watch, ["less"]);
})


gulp.task('default', function() {
    runSequence(["less"], "browsersync");
});

gulp.task("build", function() {
    runSequence("watch");
});

gulp.task('browsersync', ['build'], function() {
    browsersync({
        server: {
            baseDir: [config.dev]
        },
        port: 8080,
        files: [
            config.dest,
        ]
    });
});